package com.syezon.wifikey.config;

import com.blankj.utilcode.util.SPUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Administrator on 2018/11/12.
 */

public class AppOptConfig {

    public static final int OPT_AD_SELF = 0;
    public static final int OPT_AD_BD = 1;
    public static final int OPT_AD_GDT = 2;
    public static final int OPT_AD_TT = 3;
    public static final long OPT_INTERNAL = 1000 * 60 * 10L;
    private static final String CONFIG_NAME = "app_opt_config";
    private static final String APP_OPT_PROTECT_SET = "app_protect_set";
    private static final String OPT_TIME = "last_opt_time";
    private static final String OPT_AD_SOURCE = "opt_ad_source";

    private static final Set<String> EMPTY = new HashSet<>();

    /**
     * @param pkgName
     * @return 获取应用是否被优化保护
     */
    public static boolean isAppOptProtected(String pkgName) {
        Set<String> stringSet = SPUtils.getInstance(CONFIG_NAME).getStringSet(APP_OPT_PROTECT_SET, EMPTY);
        if (stringSet.contains("" + pkgName)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param pkgName
     * @param isProtected 保存应用是否被优化保护
     */
    public static void setAppOptProtected(String pkgName, boolean isProtected) {
        Set<String> stringSet = SPUtils.getInstance(CONFIG_NAME).getStringSet(APP_OPT_PROTECT_SET, EMPTY);
        if (isProtected) {
            stringSet.add("" + pkgName);
        } else {
            stringSet.remove("" + pkgName);
        }
        SPUtils.getInstance(CONFIG_NAME).put(APP_OPT_PROTECT_SET, stringSet);
    }

    /**
     * @return 获取优化应用保护的数量
     */
    public static int getCountAppProtectCount() {
        return SPUtils.getInstance(CONFIG_NAME).getStringSet(APP_OPT_PROTECT_SET, EMPTY).size();
    }

    /**
     * @return 获取优化的时间
     */
    public static long getOptTime() {
        return SPUtils.getInstance(CONFIG_NAME).getLong(OPT_TIME, 0L);
    }

    /**
     * @param optTime 保存优化的时间
     */
    public static void saveOptTime(long optTime) {
        SPUtils.getInstance(CONFIG_NAME).put(OPT_TIME, optTime);
    }

    /**
     * @return 获取优化完成广告来源
     */
    public static int getOptAdSource() {
        return SPUtils.getInstance(CONFIG_NAME).getInt(OPT_AD_SOURCE, OPT_AD_SELF);
    }

    /**
     * @param adSource 保存优化完成广告来源
     */
    public static void saveOptAdSource(int adSource) {
        SPUtils.getInstance(CONFIG_NAME).put(OPT_AD_SOURCE, adSource);
    }
}
